<?php
/* SVN FILE: $Id: submission.php 33 2009-10-15 13:51:32Z kenreily@gmail.com $ */
/**
 * Short description for file.
 *
 * Long description for file
 *
 * PHP versions 4 and 5
 *
 * Recenseo: Online submission and reviewing for education
 * Copyright 2008-2009, Ken Reily
 *
 * Licensed under The MIT License
 * Redistributions of files must retain the above copyright notice.
 *
 * $Date: 2009-10-15 13:51:32 +0000 (Thu, 15 Oct 2009) $
 * $Revision: 33 $
 * $Author: kenreily@gmail.com $
 * $HeadURL: http://recenseo.googlecode.com/svn/trunk/app/models/submission.php $
 */
class Submission extends AppModel {
  var $name = 'Submission';
  var $belongsTo = array('User','Assignment','GradingOption');
  var $hasMany = 'Assessment';
  var $validate = array(
    'file_name' => array(
      'rule' => array('extension', array('zip')),
      'message' => 'Your file must be a ZIP file. Please try again.'
    ),
    'grading_option_id' => array(
      'rule' => 'notEmpty',
      'message' => 'You must select a grading option.'
    ),
    'hours_est' => array('hours_rule_1' => array('rule' => 'notEmpty',
						 'message' => 'Hours must be between 1 and 50.'),
			 'hours_rule_2' => array('rule' => 'numeric',
						 'message' => 'Hours must be between 1 and 50.'),
			 'hours_rule_3' => array('rule' => array('comparison','>=',1),
						 'message' => 'Hours must be between 1 and 50.'),
			 'hours_rule_4' => array('rule' => array('comparison','<=',50),
						 'message' => 'Hours must be between 1 and 50.'))
    );
    
  function getForId($id,$user_id)
  {
    $submission = $this->query(
      "SELECT Submission.id, Course.id, Course.name, Assignment.id, Assignment.name, Assignment.due, Submission.file_name, Submission.file_type, Submission.file_size, Submission.comments, Submission.created " .
        "FROM submissions AS Submission " .
        "JOIN assignments AS Assignment ON Assignment.id = Submission.assignment_id " .
        "JOIN courses AS Course ON Course.id = Assignment.course_id " .
       "WHERE Submission.id = {$id} " .
         "AND Submission.user_id = {$user_id} " .
         "AND Submission.active = 1 "); 
                           
    if (count($submission) == 1)
    {
      return $submission[0];
    }
    else
    {
      return $submission;
    }
  }
  
  function getForDownload($id, $user_id)
  {
    return $this->query("SELECT file_name,file_type,file_size,file_data " .
                        "FROM submissions AS s WHERE s.id = {$id} AND (s.user_id = {$user_id} " .
                        "OR EXISTS (SELECT id FROM assessments ae WHERE ae.user_id = ${user_id} AND ae.submission_id = s.id) " .
                        "OR {$user_id} = 2)");
  }

  function getForAdminIndex($assignment_id)
  {
    return $this->query("SELECT users.id,users.username,users.name,submissions.id,submissions.created,submissions.comments, " .
			"(SELECT COUNT(id) FROM assessments WHERE assessments.submission_id = submissions.id AND assessments.submitted IS NOT NULL) AS assessments " .
			"FROM users " .
			"JOIN courses_users ON courses_users.user_id = users.id " .
			"JOIN assignments ON assignments.course_id = courses_users.course_id " . 
			"LEFT OUTER JOIN submissions ON submissions.user_id = users.id AND submissions.assignment_id = {$assignment_id} AND submissions.active = 1 " .
			"WHERE assignments.id = {$assignment_id} " .
			"ORDER BY users.username");

  }
}
?>